﻿/*
* MailType JavaScript Library v1.0.0
* By:bruttle
* email:hangfei1234@163.com
* Date: 2009-09-07
*/

String.prototype.TrimString = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function GetXmlDom(url) {
    try {
        var xmlDoc = null;
        if (window.ActiveXObject) {//ie
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        }
        else if (document.implementation.createDocument) {//Mozilla, Firefox, Opera, etc.
            xmlDoc = document.implementation.createDocument("", "", null);
        }
        if (!xmlDoc) {
            return null;
        }
        if (document.implementation.createDocument) {
            var xmlhttp = new window.XMLHttpRequest();
            xmlhttp.open("GET", url, false);
            xmlhttp.send(null);
            xmlDoc = xmlhttp.responseXML; 
        }
        else {
            xmlDoc.async = false;
            xmlDoc.load(url);
        }
        return xmlDoc;
    } catch (e) { }
    return null;
}

function GetnodeValue(node) {
    if (node) {
        if (document.all) {
            return node.text;
        }
        else {
            return node.textContent;
        }
    }
    return '';
}

//列表框赋值list:列表框对象，content：列表内容每项以逗号分隔，selectID：列表框初始选中项
function SetListData(list, content, val, selectID) {
    list.options.length = 0;
    var i = 0;
    var opt;
    var temp;
    list.style.display = 'inline';
    for (i = 0; i < content.length; i++) {
        opt = new Option(content[i], val[i]);
        list.options.add(opt);
        if (selectID == val[i]) {
            opt.selected = "selected";
        }
    }
}

function IsSelTextEqual(sel, selData) {
    var i=0;
    var length = selData.length;
    if (sel) {
        if(length!=sel.options.length){
            return false;
        }
        for (i = 0; i < sel.options.length; i++) {
            if (sel.options[i].text != selData[i]) {
                return false;
            }
        }
        return true;
    }
    else {
        return false;
    }
}

/*
* selID:下拉列表框ID号
* linkContainerID:联动选择连接父容器ID号
* linkIndex:需联动的数据项，采用0与1标示，0不需要联动，1需要
* startIndex:每项数据联动的是否显示-1项
* linkClass:联动链接CSS，包括选择前与后，用‘|’分割
*/
function GetMainType(selID, linkContainerID, linkClass, url,hideLink) {
    this.selID = selID;
    this.linkContainerID = linkContainerID;
    this.linkClass = linkClass;
    if (hideLink) {
        this.hideLink = hideLink;
    }
    else {
        this.hideLink = false;
    }
    var xmlDoc = GetXmlDom(url);
    this.sourceURL = GetnodeValue(xmlDoc.getElementsByTagName('SourceURL')[0]);
    var i = 0, j = 0, k = 0;
    var arr = xmlDoc.getElementsByTagName('ListItem');
    var tempArr0 = new Array(arr.length), tempArr1 = new Array(arr.length), tempArr2 = new Array(arr.length), tempArr3 = new Array(arr.length), tempArr4 = new Array(arr.length), tempArr5 = new Array(arr.length);
    for (i = 0; i < arr.length; i++) {
        tempArr0[i] = GetnodeValue(arr[i].getElementsByTagName('ItemText')[0]);
        tempArr1[i] = GetnodeValue(arr[i].getElementsByTagName('ItemValue')[0]);
        tempArr2[i] = GetnodeValue(arr[i].getElementsByTagName('ShowSelect')[0]);
        tempArr3[i] = GetnodeValue(arr[i].getElementsByTagName('ShowLink')[0]);
        tempArr4[i] = GetnodeValue(arr[i].getElementsByTagName('StartLinkText')[0]);
        tempArr5[i] = GetnodeValue(arr[i].getElementsByTagName('UrlPara')[0]);
    }
    this.firstSelText = tempArr0;
    this.firstSelValue = tempArr1;
    this.showSelect = tempArr2;
    this.showLink = tempArr3;
    this.startLinkText = tempArr4;
    this.urlPara = tempArr5;
    tempArr0 = new Array(arr.length), tempArr1 = new Array(arr.length);
    var arr1, arr2;
    for (i = 0; i < arr.length; i++) {
        arr1 = arr[i].getElementsByTagName('SubItems');
        tempArr0[i] = new Array(arr1.length);
        tempArr1[i] = new Array(arr1.length);
        for (j = 0; j < arr1.length; j++) {
            arr2 = arr1[j].getElementsByTagName('SubItem');
            tempArr0[i][j] = new Array(arr2.length);
            tempArr1[i][j] = new Array(arr2.length);
            for (k = 0; k < arr2.length; k++) {
                tempArr0[i][j][k] = GetnodeValue(arr2[k].getElementsByTagName('ItemText')[0]);
                tempArr1[i][j][k] = GetnodeValue(arr2[k].getElementsByTagName('ItemValue')[0]);
            }
        }
    }
    this.otherSelText = tempArr0;
    this.otherSelValue = tempArr1;
}

GetMainType.prototype.InitControl = function(index) {
    var sels = this.selID.split('|');
    var i, j, k;
    var obj = document.getElementById(sels[0]);
    var selIndex;
    if (index && index >= 0) {
        selIndex = index;
    }
    else {
        selIndex = 0;
    }
    var linkStartTexts = this.startLinkText[selIndex].split('|');
    if (document.getElementById(this.linkContainerID)) {
        document.getElementById(this.linkContainerID).innerHTML = '';
    }
    if (obj) {
        if (obj.options.length <= 0) {
            SetListData(obj, this.firstSelText, this.firstSelValue, this.firstSelValue[selIndex]);

        }
        if (!this.hideLink && this.showLink[selIndex].charAt(0) == '1') {
            this.SetLinkData(this.linkContainerID, this.firstSelText, this.firstSelValue, this.firstSelText[selIndex], sels[0], this.linkClass, parseInt(this.showLink[selIndex].charAt(1 + sels.length)), linkStartTexts[0]);
        }
    }
    else {
        return;
    }
    var cls = this;
    obj.onchange = function() {
        if (cls.hideLink) {
            var lc;
            if (cls.hideLink) {
                lc = cls.linkContainerID.split('|');
            }
            var k = 0;
            for (k = 0; k < lc.length; k++) {
                var t = document.getElementById(lc[k]);
                if (t) {
                    if (k == this.selectedIndex) {
                        t.style.display = 'block';
                    }
                    else {
                        t.style.display = 'none';
                    }
                }
            }
        }
        cls.InitControl(this.selectedIndex);
    }
    for (i = 1; i < sels.length; i++) {
        obj = document.getElementById(sels[i]);
        if (this.showSelect[selIndex].charAt(i) == '1') {
            if (i <= this.otherSelText[selIndex].length && obj) {
                SetListData(obj, this.otherSelText[selIndex][i - 1], this.otherSelValue[selIndex][i - 1], this.otherSelValue[selIndex][i - 1][0]);
                if (!this.hideLink && this.showLink[selIndex].charAt(i) == '1') {
                    this.SetLinkData(this.linkContainerID, this.otherSelText[selIndex][i - 1], this.otherSelValue[selIndex][i - 1], this.otherSelText[selIndex][i - 1][0], sels[i], this.linkClass, parseInt(this.showLink[selIndex].charAt(i + 1 + sels.length)), linkStartTexts[i]);
                    var linkCls = this.linkClass;
                    obj.onchange = function() {
                        var linkCon = document.getElementById(cls.linkContainerID);
                        if (linkCon) {
                            var linkButtons = linkCon.getElementsByTagName('a');
                            if (linkButtons && linkButtons.length > 0) {
                                var k = 0;
                                for (k = 0; k < linkButtons.length; k++) {
                                    if (linkButtons[k].id.indexOf(this.id + '_link') == 0) {
                                        if (linkButtons[k].innerHTML == this.options[this.selectedIndex].text) {
                                            linkButtons[k].className = linkCls.split('|')[0];
                                        }
                                        else {
                                            linkButtons[k].className = linkCls.split('|')[1];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        else {
            if (obj) {
                obj.options.length = 0;
                obj.style.display = 'none';
            }
        }
    }
}

GetMainType.prototype.GetLinkURL = function() {
    var sels = this.selID.split('|');
    var url = this.sourceURL;
    var para1 = new Array(sels.length), para2 = new Array(sels.length);
    var i;
    for (i = 0; i < sels.length; i++) {
        para1[i] = '';
        para2[i] = '';
    }
    var obj = document.getElementById(sels[0]);
    var selIndex;
    var p1 = '', p2 = '';
    if (obj) {
        if (obj.options.length > 0) {
            selIndex = obj.selectedIndex;
            for (i = 0; i < sels.length; i++) {
                if (parseInt(this.urlPara[selIndex].charAt(i)) > 0) {
                    obj = document.getElementById(sels[i]);
                    if (this.urlPara[selIndex].charAt(i + 1 + sels.length) == '1') {
                        if (obj && obj.options.length > 0 && obj.value != '-1') {
                            para1[parseInt(this.urlPara[selIndex].charAt(i))] = para1[parseInt(this.urlPara[selIndex].charAt(i))] + obj.value;
                        }
                    }
                    else if (this.urlPara[selIndex].charAt(i + 1 + sels.length) == '2') {
                        if (obj && obj.options.length > 0 && obj.value != '-1') {
                            para1[parseInt(this.urlPara[selIndex].charAt(i))] = obj.value;
                        }
                    }
                    else {
                        if (obj && obj.options.length > 0 && obj.value != '-1') {
                            if (para2[parseInt(this.urlPara[selIndex].charAt(i))] != '') {
                                para2[parseInt(this.urlPara[selIndex].charAt(i))] = para2[parseInt(this.urlPara[selIndex].charAt(i))] + '&' + obj.value;
                            }
                            else {
                                para2[parseInt(this.urlPara[selIndex].charAt(i))] = obj.value;
                            }
                        }
                    }
                }
            }
            for (i = 0; i < sels.length; i++) {
                if (para1[i] != '') {
                    p1 = p1 + para1[i] + '/';
                }
                if (para2[i] != '') {
                    if (p2 != '') {
                        p2 = p2 + '&' + para2[i] ;
                    }
                    else {
                        p2 = para2[i];
                    }
                }
            }
            url = url + p1;
            if (p2 != '') {
                url = url + '?' + p2;
            }
        }
    }
    return url;
}

GetMainType.prototype.GetSearchUrl = function(input) {
    var obj = document.getElementById(input);
    var url = '';
    try {
        if (obj) {
            if (obj.value != '' && obj.value != '搜索“车辆”相关信息' && obj.value != '可输入小区或地名') {
                var url = this.GetLinkURL();
                if (!url.match(/key=[^&]+/ig)){
                    if (url.indexOf('?') > 0) {
                        url = this.GetLinkURL() + '&key=' + encodeURI(obj.value);
                    }
                    else {
                        url = this.GetLinkURL() + '?key=' + encodeURI(obj.value);
                    }
                }
                return url;
            }
            else {
                return this.GetLinkURL();
            }
        }
        else {
            return this.GetLinkURL();
        }
    }
    catch (e) {
    }
    return 'http://www.58.com';
}

GetMainType.prototype.GetLinkURL1 = function(sID, vals) {
    var sels = this.selID.split('|');
    var url = this.sourceURL;
    var para1 = new Array(sels.length), para2 = new Array(sels.length);
    var i;
    for (i = 0; i < sels.length; i++) {
        para1[i] = '';
        para2[i] = '';
    }
    var obj = document.getElementById(sels[0]);
    var selIndex;
    var p1 = '', p2 = '';
    if (obj) {
        if (obj.options.length > 0) {
            selIndex = obj.selectedIndex;
            for (i = 0; i < sels.length; i++) {
                if (parseInt(this.urlPara[selIndex].charAt(i)) > 0 && (i == 0 || sID == sels[i])) {
                    obj = document.getElementById(sels[i]);
                    if (this.urlPara[selIndex].charAt(i + 1 + sels.length) == '1') {
                        if (i == 0) {
                            para1[parseInt(this.urlPara[selIndex].charAt(i))] = para1[parseInt(this.urlPara[selIndex].charAt(i))] + obj.value;
                        }
                        else {
                            para1[parseInt(this.urlPara[selIndex].charAt(i))] = para1[parseInt(this.urlPara[selIndex].charAt(i))] + vals;
                        }
                    }
                    else if (this.urlPara[selIndex].charAt(i + 1 + sels.length) == '2') {
                        if (i == 0) {
                            para1[parseInt(this.urlPara[selIndex].charAt(i))] = obj.value;
                        }
                        else {
                            para1[parseInt(this.urlPara[selIndex].charAt(i))] = vals;
                        }
                    }
                    else {
                        if (para2[parseInt(this.urlPara[selIndex].charAt(i))] != '') {
                            if (i == 0) {
                                para2[parseInt(this.urlPara[selIndex].charAt(i))] = para2[parseInt(this.urlPara[selIndex].charAt(i))] + '&' + obj.value;
                            }
                            else {
                                para2[parseInt(this.urlPara[selIndex].charAt(i))] = para2[parseInt(this.urlPara[selIndex].charAt(i))] + '&' + vals;
                            }
                        }
                        else {
                            if (i == 0) {
                                para2[parseInt(this.urlPara[selIndex].charAt(i))] = obj.value;
                            }
                            else {
                                para2[parseInt(this.urlPara[selIndex].charAt(i))] = vals;
                            }
                        }
                    }
                }
            }
            for (i = 0; i < sels.length; i++) {
                if (para1[i] != '') {
                    p1 = p1 + para1[i] + '/';
                }
                if (para2[i] != '') {
                    if (p2 != '') {
                        p2 = p2 + '&' + para2[i];
                    }
                    else {
                        p2 = para2[i];
                    }
                }
            }
            url = url + p1;
            if (p2 != '') {
                url = url + '?' + p2;
            }
        }
    }
    return url;
}

GetMainType.prototype.SetLinkData = function(container, content, val, selectText, selID, linkClass, startIndex, linkStartText) {
    var con = document.getElementById(container);
    var cls = linkClass.split('|');
    selectText = selectText.TrimString();
    if (con) {
        var i;
        var result = linkStartText+'：';
        for (i = startIndex; i < content.length; i++) {
            if (selectText == content[i]) {
                result = result + '<a id="' + selID + '_link' + i + '" href="javascript:void(0)" onclick="window.open(\'' + this.GetLinkURL1(selID, val[i]) + '\');SetSelectText(this,\'' + selID + '\');return false;" target="_self" class="' + cls[0] + '" >' + content[i] + '</a>&nbsp;';
            }
            else {
                result = result + '<a id="' + selID + '_link' + i + '" href="javascript:void(0)" onclick="window.open(\'' + this.GetLinkURL1(selID, val[i]) + '\');SetSelectText(this,\'' + selID + '\');return false;" target="_self" class="' + cls[1] + '" >' + content[i] + '</a>&nbsp;';
            }
        }
        if (con.innerHTML != '') {
            con.innerHTML = con.innerHTML + "<br />" + result;
        }
        else {
            con.innerHTML = result;
        }
    }
}

function SetSelectText(obj, selID) {
    if (obj && document.getElementById(selID)) {
        var list = document.getElementById(selID);
        var i;
        for (i = 0; i < list.options.length; i++) {
            if (list.options[i].text.TrimString() == obj.innerHTML.TrimString()) {
                list.options[i].selected = "selected";
                return;
            }
        }
    }
}

function SetSelectLink(container, selectText, index, linkClass) {
    var con = document.getElementById(container);
    var cls = linkClass.split('|');
    selectText = selectText.TrimString();
    if (con) {
        var arr = con.getElementsByTagName('a');
        var i;
        for (i = 0; i < arr.length; i++) {
            if (arr[i].innerHTML.TrimString() == selectText) {
                arr[i].className = cls[0];
            }
            else {
                arr[i].className = cls[1];
            }
        }
    }
}

